package com.briup.homework.chap10;

import com.briup.homework.chap10.bean.Account;
import com.briup.homework.chap10.bean.Bank;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class Test11_2_BankTest {
    public static void main(String[] args) {
        // 提供两个帐号
        Account a1 = new Account("apple",5000.0);
        Account a2 = new Account("strawberry",2000.0);
        // 初始化银行
//        Bank bank = new Bank(Arrays.asList(a1,a2));
        Bank bank = new Bank();
        // 创建十个转账线程
        for (int i = 0; i < 10; i++) {
            Thread t = new  Thread(new Runnable() {
                @Override
                public void run() {
                    // 定义一个随机数，转账的金额
                    Random random = new Random();
                    int count = 5;
                    while (count > 0) {
                        count--;
                        BigDecimal bg = BigDecimal.valueOf(random.nextDouble() * 100).
                                setScale(2, RoundingMode.HALF_UP);
                        bank.transfer(a1,a2,bg.doubleValue());
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            });
            t.start();
        }
    }
}
